<#assign basePath=request.contextPath />
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色权限分配</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="${basePath}/js/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="${basePath}/js/jquery1.8.3.min.js"></script>
    <script src="${basePath}/js/layer/layer.js"></script>
    <script src="${basePath}/js/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="${basePath}/js/shiro.demo.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <form style="margin-top: 1.5em" method="post" action="${basePath}/permit/permissionBoList" id="formId"
                  class="form-inline">
                <div clss="well">
                    <div class="form-group">
                        <input type="text" class="form-control" style="width: 300px;"
                               value="${rolePermissionBo.name?default('')}"
                               name="name" id="name" placeholder="输入角色名称 / 角色类型">
                    </div>
                    <span class=""> <#--pull-right -->
                        <button type="submit" class="btn btn-primary">查询</button>
                    </span>
                </div>

                <hr>
                <table class="table table-bordered">
                    <input type="hidden" id="selectRoleId">
                    <tr>
                        <th width="10%">角色名称</th>
                        <th width="10%">角色类型</th>
                        <th width="60%">拥有的权限</th>
                        <th width="15%">操作</th>
                    </tr>

                    <#if page?exists && page.result?size gt 0 >
                        <#list page.result as it>
                       <tr>
                           <td>${it.name}</td>
                           <td>${it.type}</td>
                           <td permissionIds="${it.permissionIds?default('')}">${it.permissionNames?default('-')}</td>
                           <td>
                               <i class="glyphicon glyphicon-share-alt"></i><a
                                   href="javascript:selectPermissionById(${it.id});">选择权限</a>
                           </td>
                       </tr>
                        </#list>
                    </#if>
                </table>
                <#if page?exists>
                <div class="pagination pull-right">
                    ${page.pageHtml}
                </div>
                </#if>
            </form>
        </div>
    </div>


<#--弹框-->
    <div class="modal fade bs-example-modal-sm" id="selectPermission" tabindex="-1" role="dialog"
         aria-labelledby="selectPermissionLabel">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="selectPermissionLabel">添加权限</h4>
                </div>
                <div class="modal-body">
                    <form id="boxRoleForm">
                        loading...
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" onclick="selectPermission();" class="btn btn-primary">保存</button>
                </div>
            </div>
        </div>
    </div>
<#--/弹框-->

</div>
</body>
<script>


    <#--选择权限后保存-->
    function selectPermission() {
        var checked = $("#boxRoleForm  :checked");
        var ids = [], names = [];
        $.each(checked, function () {
            ids.push(this.id);
            names.push($.trim($(this).attr('name')));
        });
        var index = layer.confirm("确定操作？", function () {
        <#--loding-->
            var load = layer.load();
            $.post('${basePath}/permit/addPermission2Role', {
                ids: ids.join(','),
                roleId: $('#selectRoleId').val()
            }, function (result) {
                layer.close(load);
                if (result && result.status != 200) {
                    return layer.msg(result.message, so.default), !1;
                }
                layer.msg('添加成功');
                setTimeout(function () {
                    $('#formId').submit();
                }, 1000);
            }, 'json');
        });
    }

    /*
    *根据角色ID选择权限，分配权限操作
    */
    function selectPermissionById(id) {
        var load = layer.load();
        $.post("${basePath}/permit/selectPermissionByRoleId", {id: id}, function (result) {
            layer.close(load);
            if (result && result.length) {
                var html = [];
                html.push('<div class="checkbox"><label><input type="checkbox"  selectAllBox="">全选</label></div>');
                $.each(result, function () {
                    html.push("<div class='checkbox'><label>");
                    html.push("<input type='checkbox' selectBox='' id='");
                    html.push(this.id);
                    html.push("'");
                    if (this.check) {
                        html.push(" checked='checked'");
                    }
                    html.push("name='");
                    html.push(this.name);
                    html.push("'/>");
                    html.push(this.name);
                    html.push('</label></div>');
                });
                //初始化全选
                return so.id('boxRoleForm').html(html.join('')),
                        so.checkBoxInit('[selectAllBox]', '[selectBox]'),
                        $('#selectPermission').modal(), $('#selectRoleId').val(id), !1;
            } else {
                return layer.msg('没有获取到权限数据，请先添加权限数据', so.default);
            }
        }, 'json');
    }
</script>
</html>